/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.kamai.client.upload;

import com.edu.kamai.config.Constant;
import com.edu.kamai.utility.FileName;
import com.edu.kamai.utility.FileOperation;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author xyzhu
 */
public class UploadMP3 extends Thread{
    
    public UploadMP3(){
        mp3List = new ArrayList<String>();
    }
    
    public void addMP3Path(String path){
        mp3List.add(path);
    }
    
    private String copyFile(String sFileName){
        File sFile = new File(sFileName);
        String dFileName = FileName.reAbsPathWithTime(sFileName);
        File dFile = new File(dFileName);
        //dFile.mkdir();
        
        System.out.println("dFileName:" + dFileName);
        int res = FileOperation.copyFile(sFile, dFile);
        if(1 == res){
            return dFileName;
        }
        return null;
    }
    
        
    @Override
    public void run() {
        String url = Constant.upLoadUrl;
        for(int i=0;i<mp3List.size();i++){
            String file = mp3List.get(i);
            String newFile = copyFile(file);
            if(null == newFile){
                return ;
            }
            PostFile p = new PostFile(newFile, url);
            try{
                p.uploadFile();
            }catch(Exception ex){
                ex.printStackTrace();
            }
            //deleteFile();
            FileOperation.deleteFile(newFile);
        }
        
    }
    
    private List<String> mp3List; 
    
    /*
    public static void main(String[] arg){
        String file = "c:/我是测试的.xml";
        UploadMP3 um = new UploadMP3();
        um.addMP3Path(file);
        um.start();
    } 
     * 
     */
}
